<?php
declare (strict_types=1);

namespace app\db;

use app\util\Tool;
use think\Config;
use think\facade\Db;
use think\Model;


class BigDbRush
{
    public function findRecordByxx_Rush($xx){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_record")
            ->where('id',$xx)
            ->order('id','desc')
            ->select();
        return $sqldata[0];
    }

    //findOrderbyDetailid_rush
    public function findOrderbybillcode_rush($billcode){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_order")
            ->where('ordersn',$billcode)
            ->order('id','desc')
            ->select();
        return !empty($sqldata) ? $sqldata[0] : null;
    }

    //findpidaidforfengbyphone_1_rush
    public function findpidaidforfengbyphone_1_rush($phone){
        $sqldata = \think\facade\Db::table("ims_users")
            ->where('username',$phone)
            ->order('uid','desc')
            ->select();
        return $sqldata;
    }

    //findpidaidforfengbyphone_2_rush
    public function findpidaidforfengbyphone_2_rush($uid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_theme")
            ->where('uid',$uid)
            ->select();
        return $sqldata;
    }



    //findpidaidforfengbyphone_3_rush
    public function findpidaidforfengbyphone_3_rush($uid,$aid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_theme")
            ->where('uid',$uid)
            ->where('id',$aid)
            ->select();
        return $sqldata;
    }




    //用id =1 可以测db工作正常不正常
    public function findOrderBytid2_rush($tid2){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_order")
            ->where('ordersn',$tid2)
            ->order('id','desc')
            ->select();
//        $arr['sqldata'] = $sqldata;
//        $arr['ask'] = "findOrderBytid2";
        //说不定会报错哦

        return !empty($sqldata) ? $sqldata[0] : null;
    }

    //findThemebyid_rush
    public function findThemebyid_rush($id){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_theme")
            ->where('id',$id)
            ->order('id','desc')
            ->select();
        return !empty($sqldata) ? $sqldata[0] : null;
    }


    //findOrderByShortid_rush
    //订单表 DY
    public function findOrderByShortid_rush($shortid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_record")
            ->where('id',$shortid)
            ->order('id','desc')
            ->select();
        return !empty($sqldata) ? $sqldata[0] : null;
    }


    //findaidsbypid_rush
    public function findaidsbypid_rush($pid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_theme")
            ->where('uniacid',$pid)
            ->select();
        return $sqldata;
    }

    //pcaids_totalfee_rush
    public function pcaids_totalfee_rush($themeids_arr)
    {
        $sqldata = Db::table("ims_water_groupbuy3_record")
            ->whereIn('themeid',  $themeids_arr)
            ->whereIn('status',[1,2])
            ->group('themeid')
            ->field('themeid, SUM(fee) AS total_fee')
            ->select()
            ->toArray();

        return $sqldata;
    }

    //
    public function pcaids_commfee_rush($themeids_arr)
    {
        $sqldata = Db::table("ims_water_groupbuy3_comm")
            ->whereIn('themeid',  $themeids_arr)
            ->whereIn('status',[1])
            ->group('themeid')
            ->field('themeid, SUM(fee) AS comm_fee')
            ->select()
            ->toArray();

        return $sqldata;
    }

    //pcaids_other_rush

    //pcaids_other
    public function pcaids_other_rush($themeids_arr)
    {
        $sqldata = Db::table("ims_water_groupbuy3_theme")
            ->whereIn('id',  $themeids_arr)
            ->group('id')
            ->field('id as themeid, sum(paynum) as total_paynum,sum(fanssum) AS total_fanssum,sum(scansum) as total_scansum')
            ->select()
            ->toArray();

        return $sqldata;
    }

    //other?
    //pcaids_other
    public function pcaids_other($themeids_arr)
    {
        $sqldata = Db::table("ims_water_groupbuy3_theme")
            ->whereIn('id',  $themeids_arr)
            ->group('id')
            ->field('id as themeid, sum(paynum) as total_paynum,sum(fanssum) AS total_fanssum,sum(scansum) as total_scansum')
            ->select()
            ->toArray();

        return $sqldata;
    }

    //findcasebypid_rush
    public function findcasebypid_rush($pid)
    {
        $sqldata = Db::table("ims_water_groupbuy3_theme")
            ->where('uniacid',$pid)
            ->where('iscase',1)
            ->select()
            ->toArray();

        return $sqldata;
    }

    public function findphonebyaid_1_rush($aid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_theme")
            ->where('id',$aid)
            ->select();
        return $sqldata;
    }

    //findphonebyaid_2
    public function findphonebyaid_2_rush($uid){
        $sqldata = \think\facade\Db::table("ims_users")
            ->where('uid',$uid)
            ->select();
        return $sqldata;
    }

    //findformsetbypidandaid
    public function findformsetbypidandaid_rush($pid,$aid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_set")
            ->where('uniacid',$pid)
            ->where('key',$aid)
            ->select();
        return $sqldata;
    }

    //findthemeinfobypidaid
    public function findthemeinfobypidaid_rush($pid,$aid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_theme")
            ->where('id',$aid)
            ->where('uniacid',$pid)
            ->select();
        return $sqldata;
    }

    public function findconfigbypidaid_rush($pid,$aid){
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_config")
            ->where('uniacid',$pid)
            ->where('key',$aid)
            ->select();
        return $sqldata;
    }

    //the_worklist_rush
    public function the_worklist_rush($uniacid,$themeid)
    {
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_worker")
            ->where('uniacid',$uniacid)
            ->where('themeid',$themeid)
//            ->fetchSql(true)
            ->order('id','desc')
            ->select();

        $arr['sqldata'] = $sqldata;
        $arr['ask'] = "the_worklist_rush";
        return $arr;
    }


    //
    public function findhaibao_touxiang_rush($uniacid,$themeid,$openid)
    {
        $needu = 'neeedu'; //平台ID uniacid
        $needt = 'needt';  //活动themeid t
        //$needo = "needo";  //openid
        $orginsql = "SELECT * from ims_water_groupbuy3_fans  where uniacid = neeedu and themeid = needt and  openid = '". $openid  ."' limit 1 ";
        $orginsql = str_ireplace($needu, $uniacid, $orginsql);
        $orginsql = str_ireplace($needt, $themeid, $orginsql);
        $sqldata = \think\facade\Db::query($orginsql);
        $arr['sql'] = $orginsql;
        $arr['sqldata'] = $sqldata;
        $arr['ask'] = "findhaibao";
        return $arr;
    }

    public function findhaibao_config_rush($uniacid,$themeid)
    {
        $needu = 'neeedu'; //平台ID uniacid
        $needt = 'needt';  //活动themeid t
        //DB设计有问题 活动themeid 又变成aic了
        $orginsql = "SELECT * from ims_water_groupbuy3_config  where uniacid = neeedu and `key` = needt   limit 1 ";
        $orginsql = str_ireplace($needu, $uniacid, $orginsql);
        $orginsql = str_ireplace($needt, $themeid, $orginsql);
        $sqldata = \think\facade\Db::query($orginsql);
        $arr['sql'] = $orginsql;
        $arr['sqldata'] = $sqldata;
        $arr['ask'] = "findhaibao_config";
        return $arr;
    }

    //finddidpid_byphone_rush
    public function finddidpid_byphone_rush($phone)
    {
        $result = Db::table('ims_water_groupbuy3_theme')
            ->alias('t')
            ->join('ims_users u', 't.uid = u.uid', 'LEFT')
            ->where('u.username', '=', $phone)
            ->group('t.uniacid')           // 按 t.uniacid 分组
            ->field('t.*')
            ->select();
        return $result;
    }

    //gettidby39like_rush
    public function gettidby39like_rush($key)
    {
        //ims_water_salegroup3_worker
        //`ims_water_salegroup3_order
        $sqldata = \think\facade\Db::table("ims_water_groupbuy3_order")
            ->where('detailid',$key)
            ->order('id','desc')
            ->select();

        $arr['sqldata'] = $sqldata;
        $arr['ask'] = "gettidby39like";
        return $arr;
    }



}
